package com.tf_cut.backend.request.xml;

import com.tf_cut.backend.request.MybankApiException;
import com.tf_cut.backend.request.MybankResponse;
import com.tf_cut.backend.request.util.JaxbUtil;
import com.tf_cut.backend.seller.enums.MybankApiExceptionEnum;

/**
 * XML格式转换器
 */
public class XmlConverter implements Converter {

    private XmlConverter() {
    }

    public static XmlConverter getInstance() {
        return XmlConverterInstance.instance;
    }

    private static class XmlConverterInstance {

        static XmlConverter instance = new XmlConverter();

    }

    @Override
    public <T extends MybankResponse> T toResponse(String responseXml, Class<T> clazz) throws MybankApiException {
        if (!XmlUtils.isXmlDocument(responseXml))
            throw new MybankApiException(MybankApiExceptionEnum.NOT_XML_TYPE);
        return getModelFromXML(responseXml, clazz);
    }

    private <T> T getModelFromXML(String responseXml, Class<T> clazz) throws MybankApiException {
        return JaxbUtil.converyToJavaBean(responseXml, clazz);
    }
}